//
// File   : Flight.cpp
// Author : Dylan Gleason
// Class  : CST236 - Software Testing
//
// Implementation of the Flight class. Flight
// represents Flight information data to be
// added to flight database.
//

#include "Flight.h"
#include <iostream>
#include <iomanip>


Flight::Flight(const std::string& flight, const std::string& departure,
	const std::string& arrival, Status status)
	: m_flightNumber(flight)
	, m_departureTime(departure)
	, m_arrivalTime(arrival)
	, m_flightStatus(status)
	, m_firstClass()
	, m_coachClass()
	, m_numPassengers(0)
	, m_coachCount(1)
	, m_fcCount(1)
{
	initializeSeatingChart();
}

//
// Accessors
//
const std::string& Flight::getFlightNumber() const
{
	return m_flightNumber;
}

const std::string& Flight::getDepartureTime() const
{
	return m_departureTime;
}

const std::string& Flight::getArrivalTime() const
{
	return m_arrivalTime;
}

int Flight::getNumPassengers() const
{
	return m_numPassengers;
}

Flight::Status Flight::getFlightStatus() const
{
	return m_flightStatus;
}

int Flight::getFirstClassCount() const
{
	return m_fcCount;
}

int Flight::getCoachClassCount() const
{
	return m_coachCount;
}

//
// Mutators
//
void Flight::setFlightNumber(const std::string& val)
{
	m_flightNumber = val;
}

void Flight::setDepartureTime(const std::string& val)
{
	m_departureTime = val;
}

void Flight::setArrivalTime(const std::string& val)
{
	m_arrivalTime = val;
}

void Flight::setFlightStatus(Status val)
{
	m_flightStatus = val;
}

void Flight::addFirstClassSeat(const std::string& passenger)
{
	m_firstClass[m_fcCount] = passenger;
	++m_numPassengers;
	++m_fcCount;
}

void Flight::addCoachClassSeat(const std::string& passenger)
{
	m_coachClass[m_coachCount] = passenger;
	++m_numPassengers;
	++m_coachCount;
}

bool Flight::removeFirstClassSeat(const std::string& passenger)
{
	SeatingChartIter iter;

	for(iter = m_firstClass.begin(); iter != m_firstClass.end(); ++iter)
	{
		if (iter->second == passenger)
		{
			iter->second = "";
			--m_numPassengers;
			--m_fcCount;
			return true;
		}
	}

	return false;
}

bool Flight::removeCoachClassSeat(const std::string& passenger)
{
	SeatingChartIter iter;

	for(iter = m_coachClass.begin(); iter != m_coachClass.end(); ++iter)
	{
		if (iter->second == passenger)
		{
			iter->second = "";
			--m_numPassengers;
			--m_coachCount;
			return true;
		}
	}

	return false;
}

//
// print : void
//
// method will print the flight number, flight departure time, flight arrival
// time, and the flight status for the given flight.
//
void Flight::print() const
{
	std::string status;

	if (getFlightStatus() == ONTIME)
		status = "ONTIME";
	if (getFlightStatus() == DELAYED)
		status = "DELAYED";
	if (getFlightStatus() == CANCELLED)
		status = "CANCELLED";

	std::cout << std::left
		<< std::setw(20) << getFlightNumber() 
		<< std::setw(20) << getDepartureTime() 
		<< std::setw(20) << getArrivalTime() 
		<< status 
		<< std::endl;
}

//
// printFirstClassSeats : void
//
// method will print all of the first class seats to the console, including
// the seat number and the name of passenger if occupied
//
void Flight::printFirstClassSeats() const
{
	SeatingChartIterC iter;

	std::cout << std::endl << "====== " << "First Class Seats" << " ======" << std::endl;

	for(iter = m_firstClass.begin(); iter != m_firstClass.end(); ++iter)
		std::cout << iter->first << ": " << iter->second << std::endl;
	std::cout << std::endl;
}

//
// printCoachClassSeats : void
// 
// method will print all of the coach class seats to the console, including 
// the seat number and the name of passenger if occupied
//
void Flight::printCoachClassSeats() const
{
	SeatingChartIterC iter;

	std::cout << std::endl << "====== " << "Coach Class Seats" << " ======" << std::endl;

	for(iter = m_coachClass.begin(); iter != m_coachClass.end(); ++iter)
		std::cout << iter->first << ": " << iter->second << std::endl;
	std::cout << std::endl;
}

//
// initializeSeatingChart : void
//
// Will loop through each seating chart and initialize all of the seating chart
// data.
//
void Flight::initializeSeatingChart()
{
	for(int i = 0; i < MAX_SEATS; ++i)
	{
		// key for the seating chart will be an integer value from 1 to MAX_SEATS-1
		m_firstClass.insert(std::make_pair(i+1, ""));
		m_coachClass.insert(std::make_pair(i+1, ""));
	}
}
